package cn.lu.service.impl;

import cn.lu.dao.CurriculumMapper;
import cn.lu.dao.UserCurriculumMapper;
import cn.lu.dao.UserMapper;
import cn.lu.pojo.Curriculum;
import cn.lu.pojo.UserCurriculum;
import cn.lu.service.CurriculumService;
import cn.lu.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CurriculumServiceImpl implements CurriculumService {
    @Autowired
    private CurriculumMapper curriculumMapper;

    @Autowired
    private UserCurriculumMapper userCurriculumMapper;
    @Autowired
    private UserMapper userMapper;


    @Override
    public Result addCurriculum(Curriculum curriculum) {
        if (curriculumMapper.insert(curriculum)==1){
            return new Result().success();
        } else {
            return new Result().fail();
        }
    }

    @Override
    public List<Curriculum> selectByIdAndUserId(Integer curriculumId, Integer userId) {
        return curriculumMapper.selectByIdAndUserId(curriculumId, userId);
    }

    @Override
    public int deleteByIdAndUserId(Integer curriculumId, Integer userId) {
        return curriculumMapper.deleteByIdAndUserId(curriculumId, userId);
    }

    @Override
    public Result updateByIdAndUserId(Curriculum curriculum, Integer userId) {
        if (curriculumMapper.updateByIdAndUserId(curriculum, userId)==1){
            return new Result().success();
        } else {
            return new Result().fail();
        }
    }


    /**
     * 老师创建课程
     *
     * @param curriculum
     * @param userId
     * @return
     */
    @Override
    public Result addInTheUserId(Curriculum curriculum, Integer userId) {
        if (userMapper.userWhetherTeacher(userId) == 1) {
            curriculumMapper.insert(curriculum);
            UserCurriculum userCurriculum = new UserCurriculum();
            userCurriculum.setUserId(userId);
            userCurriculum.setCurriculumId(curriculum.getId());
            if (userCurriculumMapper.insert(userCurriculum)== 1) {
                return new Result().success();
            } else {
                return new Result().fail();
            }
        } else {
            return new Result().fail("您没有权限操作");
        }
    }
}